package me.leiusual.sharing.widget

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.forEach
import kotlinx.android.synthetic.main.layout_state.view.*
import me.leiusual.sharing.R
import me.leiusual.sharing.util.extension.switchView

/**
 * author: leiusual
 * create on: 2019-07-25
 * description:
 */
class StateConstraintLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {

  private val contentIds = mutableListOf<Int>()

  override fun onFinishInflate() {
    super.onFinishInflate()
    forEach {
      contentIds.add(it.id)
    }
    View.inflate(context, R.layout.layout_state, this)
  }

  fun showProgress() {
    switchView(R.id.content_loading_progress_bar)
  }

  fun showError(message: String?) {
    text_view_error.text = message ?: "发生了未知错误"
    switchView(R.id.text_view_error)
  }

  fun showEmpty(message: String? = null) {
    text_view_empty.text = message ?: "暂无信息"
    switchView(R.id.text_view_empty)
  }

  fun showContent() {
    switchView(contentIds)
  }
}